package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.Userauthorder;
import com.qcxk.yuqian.system.service.IUserauthorderService;
import com.qcxk.yuqian.system.utils.RedisUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;


/**
 * @auther lp
 * @create 2021-05-24 02:34:15
 * @describe 存储每个用户自定义的一级菜单的顺序前端控制器
 */
@RestController
@RequestMapping("/userauthorder")
public class UserauthorderController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IUserauthorderService userauthorderService;

    @BehaviourOperator(desc = "查询Userauthorder信息")
    @RequestMapping(value = "/loadUserauthorder")
    public JSONObject loadUserauthorder(Userauthorder userauthorder) {
        JSONObject json = new JSONObject();
        userauthorder = userauthorderService.getById(userauthorder.getId());
        if (userauthorder != null) {
            json.put("code", "0");
            json.put("data", userauthorder);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询Userauthorder列表")
    @RequestMapping(value = "/queryUserauthorderList")
    public JSONObject queryUserauthorderList(Page page, Userauthorder userauthorder) {
        JSONObject json = new JSONObject();
        page = userauthorderService.queryUserauthorderPage(page, userauthorder);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());

        List<Userauthorder> list = page.getRecords();

        json.put("dataList", list);
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改Userauthorder")
    @RequestMapping(value = "/updateUserauthorderSingle")
    public JSONObject updateUserauthorderSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        Userauthorder userauthorder = userauthorderService.getById(id);
        Class<? extends Userauthorder> clazz = userauthorder.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(userauthorder, value);
        if (userauthorderService.updateById(userauthorder)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "整体修改Userauthorder")
    @RequestMapping(value = "/updateUserauthorderMultiple")
    public JSONObject updateUserauthorderMultiple(Userauthorder userauthorder) {
        JSONObject json = new JSONObject();

        if (userauthorderService.updateById(userauthorder)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除Userauthorder")
    @RequestMapping(value = "/deleteUserauthorderById")
    public JSONObject deleteUserauthorderById(Userauthorder userauthorder) {
        JSONObject json = new JSONObject();
        if (userauthorderService.deleteUserauthorderById(userauthorder)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增Userauthorder")
    @RequestMapping(value = "/addUserauthorder")
    public JSONObject addUserauthorder(Userauthorder userauthorder) {
        JSONObject json = new JSONObject();
        if (userauthorderService.save(userauthorder)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

}

